import { message } from 'antd';
import fetch from '../utils/request';

const queryAutomaticList = ({ payload }) => fetch.post('/haierp1/automaticOrder/queryAutomatic', { data: payload }).catch(e => e);
const queryUpdateAutomatic = ({ payload }) => fetch.post('/haierp1/automaticOrder/queryUpdateAutomatic', { data: payload }).catch(e => e);
const addAutomatic = ({ payload }) => fetch.post('/haierp1/automaticOrder/addAutomatic', { data: payload }).catch(e => e);
const UpdateAutomatic = ({ payload }) => fetch.post('/haierp1/automaticOrder/UpdateAutomatic', { data: payload }).catch(e => e);
const deleteAutomatic = ({ payload }) => fetch.post('/haierp1/automaticOrder/deleteAutomatic', { data: payload }).catch(e => e);
export default {
  namespace: 'automatic',
  state: {
    automaticList: [],
    automaticPage: 1,
    automaticPageSize: 20,
    automaticTotal: 1,
    automaticValues: {},
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/automaticOrder/automaticOrderList' && !window.existCacheState('/automaticOrder/automaticOrderList')) {
          setTimeout(() => {
            dispatch({ type: 'queryAutomaticList', payload: {} });
          }, 0);
        }
      });
    },
  },

  effects: {
    * queryAutomaticList({ payload }, { call, put, select }) { // SKU管理列表
      let pageIndex = yield select(({ automatic }) => automatic.automaticPage);
      let pageSize = yield select(({ automatic }) => automatic.automaticPageSize);
      if (payload.pageIndex) {
      	pageIndex = payload.pageIndex;
        yield put({ type: 'saveAutomaticPage', payload });
      }
      if (payload.pageSize) {
        pageSize = payload.pageSize;
        yield put({ type: 'saveAutomaticPageSize', payload });
      }
      const data = yield call(queryAutomaticList, { payload: { ...payload, pageIndex, pageSize } });
      if (data.success) {
        yield put({ type: 'updateAutomaticList', payload: data, });
      }
    },
    * queryUpdateAutomatic({ payload }, { call, put }) {
      const data = yield call(queryUpdateAutomatic, { payload });
      if (data.success) {
        yield put({
          type: 'saveAutomatic',
          payload: data,
        });
      }
    },
    * addAutomatic({ payload, cb }, { call }) {
      const data = yield call(addAutomatic, { payload });
      if (data.success) {
        message.success('增加自动下单任务成功');
        cb();
      }
    },
    * UpdateAutomatic({ payload, cb }, { call }) {
      const data = yield call(UpdateAutomatic, { payload });
      if (data.success) {
        message.success('更新自动下单任务成功');
        cb();
      }
    },
    * deleteAutomatic({ payload, cb }, { call }) {
      const data = yield call(deleteAutomatic, { payload });
      if (data.success) {
        message.success('删除自动下单任务成功');
        cb();
      }
    },
  },

  reducers: {
    saveAutomaticPage(state, { payload }) {
      return { ...state, automaticPage: payload.pageIndex };
    },
    saveAutomaticPageSize(state, { payload }) {
      return { ...state, automaticPageSize: payload.pageSize };
    },
    updateAutomaticList(state, { payload }) {
      return { ...state, automaticList: payload.data, automaticTotal: payload.totalCount };
    },
    saveAutomatic(state, { payload }) {
      return { ...state, automaticValues: payload };
    },
  },
};
